import request from "@/utils/request";
import { getToken } from "@/utils/auth";
import axios from "axios";
import store from "@/store";
import host from "@/utils/host";

function dateFormat(date) {
  let year = date.getFullYear();
  let month = (date.getMonth() + 1).toString().padStart(2, "0"); // 月份从 0 开始，需要加 1
  let day = date.getDate().toString().padStart(2, "0");
  return `${year}-${month}-${day}`;
}

export default {
  list(currentPage, pageSize, barcode, name, partNo, consumeFlag) {
    consumeFlag = consumeFlag || 0;
    barcode = barcode || "-1";
    name = name || "_";
    partNo = partNo || "_";
    return request({
      url: `/tool/list/${currentPage}/${pageSize}/${barcode}/${name}/${partNo}/${consumeFlag}`,
      method: "get",
    });
  },
  total(barcode, name, partNo, consumeFlag) {
    consumeFlag = consumeFlag || 0;
    barcode = barcode || "-1";
    name = name || "_";
    partNo = partNo || "_";
    return request({
      url: `/tool/total/${barcode}/${name}/${partNo}/${consumeFlag}`,
      method: "get",
    });
  },
  add(data) {
    return request({
      url: `/tool/add`,
      method: "post",
      data,
    });
  },
  update(data) {
    /**
     * 如果传来的日期是用户设置的日期，
     * 其格式将会是 Fri Mar 15 2024 00:00:00 GMT+0800 (中国标准时间)这样的时间
     * 要将这样的日期改成2023-05-16这样的格式
     */
    if (data.expireDate != null && data.expireDate.length == undefined) {
      data.expireDate = dateFormat(data.expireDate);
    }
    if (data.lastInspDate != null && data.lastInspDate.length == undefined) {
      data.lastInspDate = dateFormat(data.lastInspDate);
    }
    return request({
      url: `/tool/update`,
      method: "post",
      data,
    });
  },
  delete(data) {
    return request({
      url: `/tool/delete/${data}`,
      method: "post",
    });
  },
  isolate(id, status) {
    return request({
      url: `/tool/isolate/${id}/${status}`,
      method: "put",
    });
  },
  download() {
    let jwt = "your_jwt_string";
    if (store.getters.token) {
      jwt = getToken();
    }
    const url = `http://${host}:8081/T01/tool/download/all`;
    axios
      .get(url, {
        headers: {
          Authorization: `${jwt}`, // 在请求头中添加 JWT 字符串
          "Content-Type": "application/json", // 在请求头中添加 Content-Type
        },
        responseType: "blob", // 指定响应类型为二进制数据
      })
      .then((response) => {
        const blob = new Blob([response.data], {
          type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // 设置为正确的 MIME 类型
        });
        const url = window.URL.createObjectURL(blob);
        const link = document.createElement("a");
        link.href = url;
        link.setAttribute("download", `工具清单.xlsx`);
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      })
      .catch((error) => {
        alert("下载文件出错：" + error.message || error.data);
      });
  },
  getUnUsedTool() {
    return request({
      url: `/tool/getUnUsedTool`,
      method: "get",
    });
  },
  deleteImage(id) {
    return request({
      url: `/tool/image/deleteImage/${id}`,
      method: "delete",
    });
  },
  /**
   * 获取总数小于20的耗材
   * @returns
   */
  getHangCaiList() {
    return request({
      url: `/tool/getHangCaiList`,
      method: "get",
    });
  },
};
